import 'dart:convert';
import 'package:flutter_test/flutter_test.dart';
import 'package:body_measurement/models/base_response.dart';
import 'package:body_measurement/models/token_info.dart';
import 'package:body_measurement/models/login_result.dart';
import 'package:body_measurement/models/user_info.dart';
import 'package:body_measurement/models/measuring_list_result.dart';

void main() {
  test('test TokenInfo deserialize', () {
    const tokenInfoJsonText = '''
      {
        "errcode": 0,
        "message": "",
        "result": {
          "X-Token": "ltu49fllpieundovf4j00hogk7",
          "expire": 1296000
        }
      }''';

    BaseResponse<TokenInfo> res =
        BaseResponse.fromJson(json.decode(tokenInfoJsonText), (json) => TokenInfo.fromJson(json));
    TokenInfo token = res.result;
    expect(token.xToken, 'ltu49fllpieundovf4j00hogk7');
    expect(token.expire, 1296000);
  });

  group('test LoginResult', () {
    test('Test UserInfo deserialization when login is successful', () {
      const jsonText = '''
      {
        "errcode": 0,
        "message": "",
        "result": {
            "user_info": {
                "id": 172,
                "boss_id": 172,
                "user_type": 2,
                "username": "18972798619",
                "truename": "卫行",
                "mobilephone": "006",
                "password": "c6f44a7b265c3664f092535e21465cbf",
                "role_id": 12,
                "department": null,
                "position": null,
                "employee_number": "",
                "gender_id": 1,
                "company_name": "美红",
                "shop_name": null,
                "shop_number": null,
                "address": "1",
                "money": "1130.00",
                "pay_password": "fcea920f7412b5da7be0cf42b8c93759",
                "headimg": null,
                "wechat": "",
                "email": null,
                "qq": null,
                "remark": null,
                "status_id": 1,
                "last_login": "2024-02-29 13:00:43",
                "last_ip": "111.174.78.76",
                "create_time": "2021-06-17 14:24:25",
                "update_time": "2024-02-29 13:00:43",
                "delete_time": null
            }
        }
      }''';
      var res = BaseResponse.fromJson(json.decode(jsonText), (json) => LoginResult.fromJson(json));
      UserInfo userInfo = res.result.userInfo!;
      expect(userInfo.id, 172);
      expect(userInfo.username, '18972798619');
    });

    test('Test UserInfo deserialization when login fails', () {
      const jsonText = '''
        {
          "errcode": 10007,
          "message": "请退出当前账号再登录",
          "result": []
        }
      ''';
      var res = BaseResponse.fromJson(json.decode(jsonText), (jsonT) {
        if (jsonT is List) {
          return null;
        }

        return LoginResult.fromJson(jsonT);
      });
      var userInfo = res.result?.userInfo;
      expect(userInfo, null);
    });
  });

  test('Test task list deserialization', () {
    const jsonText = '''
      {
          "errcode": 0,
          "message": "",
          "result": {
              "count": 1,
              "list": [
                  {
                      "id": 129,
                      "user_id": 172,
                      "client_id": 409,
                      "name": "湖北省检察院",
                      "order_status": 3,
                      "order_items": [
                          {
                              "category_id": 1,
                              "category": "男西服",
                              "modeling_id": 33,
                              "modeling": "MH10",
                              "quantity": 1
                          },
                          {
                              "category_id": 2,
                              "category": "男西裤",
                              "modeling_id": 19,
                              "modeling": "MH10-2P",
                              "quantity": 1
                          },
                          {
                              "category_id": 28,
                              "category": "男衬衫",
                              "modeling_id": 89,
                              "modeling": "MC6",
                              "quantity": 1
                          },
                          {
                              "category_id": 33,
                              "category": "女西服",
                              "modeling_id": 117,
                              "modeling": "1T02",
                              "quantity": 1
                          },
                          {
                              "category_id": 34,
                              "category": "女西裤",
                              "modeling_id": 120,
                              "modeling": "0012083",
                              "quantity": 1
                          },
                          {
                              "category_id": 36,
                              "category": "女衬衫",
                              "modeling_id": 127,
                              "modeling": "00611029",
                              "quantity": 1
                          }
                      ],
                      "remark": null,
                      "create_time": "2024-01-22 07:01:09",
                      "update_time": "2024-01-22 07:01:22",
                      "user_name": "卫行",
                      "body_items": [
                          "胸围",
                          "中腰",
                          "肩宽",
                          "左袖长",
                          "衣长",
                          "腰围",
                          "裤长",
                          "脚口",
                          "臀围",
                          "袖肥"
                      ],
                      "special_items": [
                          "肩斜"
                      ],
                      "enable_delete": 1,
                      "enable_edit": 1,
                      "measurers": [
                          "杨长进"
                      ],
                      "stats": {
                          "total": 13,
                          "measured": 2,
                          "alteration": 0,
                          "repairs": 0
                      }
                  }
              ]
          }
      }
      ''';
    var res = BaseResponse.fromJson(json.decode(jsonText), (json) => MeasuringListResult.fromJson(json));
    expect(res.errcode, 0);
    expect(res.message, '');
    expect(res.result.count, 1);
    expect(res.result.taskList.length, 1);
    expect(res.result.taskList[0].id, 129);
    expect(res.result.taskList[0].name, '湖北省检察院');
    expect(res.result.taskList[0].orderItems[0].category, '男西服');
    expect(res.result.taskList[0].orderItems[0].modeling, 'MH10');
    expect(res.result.taskList[0].orderItems[1].category, '男西裤');
    expect(res.result.taskList[0].orderItems[1].modeling, 'MH10-2P');
    expect(res.result.taskList[0].remark, null);
    expect(res.result.taskList[0].bodyItems[0], '胸围');
    expect(res.result.taskList[0].bodyItems[1], '中腰');
    expect(res.result.taskList[0].specialItems, ['肩斜']);
    expect(res.result.taskList[0].measurers, ['杨长进']);
    expect(res.result.taskList[0].stats.total, 13);
    expect(res.result.taskList[0].stats.measured, 2);
  });
}
